package com.shine.icis.action;

import java.util.List;

import com.shine.esim.struts.webutil.SessionParam;
import com.shine.icis.blo.IFlowService;
import com.shine.icis.blo.ISubmitLogService;
import com.shine.icis.blo.ISubmitTaskService;
import com.shine.icis.config.SysConstants;
import com.shine.icis.hbean.SubmitReport;
import com.shine.icis.hbean.TaskIns;
import com.shine.icis.util.EFRameInf;

public class SubmitTaskAction extends BaseAction {

	/**
	 * 
	 */
	private static final long serialVersionUID = 7638121893867812633L;
	
	private ISubmitTaskService submitTaskService;
	private ISubmitLogService submitLogService;
	private IFlowService flowService;
	
	// input param
	private String taskId;
	private String comment;
	private Boolean passed;
	
	//output value
	private SubmitReport submitReport;
	private boolean firstNode;
	private List<TaskIns> taskInses;

	public void setSubmitTaskService(ISubmitTaskService submitTaskService) {
		this.submitTaskService = submitTaskService;
	}
	public void setSubmitLogService(ISubmitLogService submitLogService) {
		this.submitLogService = submitLogService;
	}
	public void setFlowService(IFlowService flowService) {
		this.flowService = flowService;
	}
	public void setTaskId(String taskId) {
		this.taskId = taskId;
	}
	public void setComment(String comment) {
		this.comment = comment;
	}
	
	public void setPassed(Boolean passed) {
		this.passed = passed;
	}
	public SubmitReport getSubmitReport() {
		return submitReport;
	}

	public boolean isFirstNode() {
		return firstNode;
	}
	
	public List<TaskIns> getTaskInses() {
		return taskInses;
	}
	/**
	 * 加载报送配置
	 * @return
	 */
	public String initQueryTask() {
		try {
			SessionParam user = (SessionParam)getSession().getAttribute(SysConstants.SESSION_USER_KEY);
			submitReport = submitTaskService.getLastSubmitReport(user.getUSER_CODE());
			if (submitReport != null) {
				submitReport.setSubmitLog(submitLogService.getSubmitLogById(submitReport.getSubmitLogId()));
				firstNode = flowService.csFirstNode(submitReport.getTaskId());
				taskInses = flowService.getEndTasks(submitReport.getInsId());
			}
			return success();
		} catch(Exception e) {
			e.printStackTrace();
			return error();
		}
	}
	
	/**
	 * 处理审批
	 * @return
	 */
	public String dealApproveTask() {
		if (taskId == null || taskId.trim().equals("")) {
			return error();
		}
		boolean success = false;
		SessionParam user = (SessionParam)getSession().getAttribute(SysConstants.SESSION_USER_KEY);
		try {
			submitTaskService.txDealApproveTask(taskId, comment, passed, user.getUSER_CODE());
			success = true;
			return success();
		} catch(Exception e) {
			e.printStackTrace();
			return error(e.getMessage());
		} finally {
			//记录系统日志
			EFRameInf.saveOpLog(user.getUSER_CODE(), EFRameInf.FUNCCODE_APPROVAL, "报送任务审核，任务ID为[" + taskId + "]，是否通过为[" + passed + "]", success);
		}
	}
	
	/**
	 * 处理审批
	 * @return
	 */
	public String cancelTask() {
		if (taskId == null || taskId.trim().equals("")) {
			return error();
		}
		boolean success = false;
		SessionParam user = (SessionParam)getSession().getAttribute(SysConstants.SESSION_USER_KEY);
		try {
			submitTaskService.txCancelTask(taskId, comment, user.getUSER_CODE());
			success = true;
			return success();
		} catch(Exception e) {
			e.printStackTrace();
			return error();
		} finally {
			
			//记录系统日志
			EFRameInf.saveOpLog(user.getUSER_CODE(), EFRameInf.FUNCCODE_APPROVAL, "报送任务审核，作废任务ID为[" + taskId + "]", success);
		}
	}

}
